//
//  ScroingTableViewController.m
//  LanQiuQuan
//
//  Created by TonyGenius on 16/4/5.
//  Copyright © 2016年 Rip. All rights reserved.
//

#import "ScroingTableViewController.h"
#import "Marco.h"

@interface ScroingTableViewController ()
{
    CGPoint _lastPoint;
    CGRect _originRect;
    CGPoint _originCenter;
    float _width;
    float _height;
    float _originWidth;
    float _originHeight;
    
    float _switchHeight;
    float _switchOriginHeight;
    NSInteger _selectedTag;
    NSInteger _selectedPlayerTag;
    NSInteger _switchTag;
}
#pragma mark -球员

@property (weak, nonatomic) IBOutlet playerView *homeSF;

@property (weak, nonatomic) IBOutlet playerView *homeC;

@property (weak, nonatomic) IBOutlet playerView *homePF;

@property (weak, nonatomic) IBOutlet playerView *homeSG;

@property (weak, nonatomic) IBOutlet playerView *homePG;

@property (weak, nonatomic) IBOutlet playerView *guestPG;

@property (weak, nonatomic) IBOutlet playerView *guestSG;

@property (weak, nonatomic) IBOutlet playerView *guestSF;

@property (weak, nonatomic) IBOutlet playerView *guestC;

@property (weak, nonatomic) IBOutlet playerView *guestPF;


#pragma mark -篮球数据
//每节时间
@property (weak, nonatomic) IBOutlet UILabel *time;

//主队图标
@property (weak, nonatomic) IBOutlet UIImageView *homeTeamImage;

//记分板背景
@property (weak, nonatomic) IBOutlet UIImageView *scoreboardImage;

//客队图标
@property (weak, nonatomic) IBOutlet UIImageView *guestTeamImage;

//节数
@property (weak, nonatomic) IBOutlet UILabel *period;

@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *scoringTableArray;

//@property (nonatomic, strong) NSMutableDictionary *playerData;

@property (weak, nonatomic) IBOutlet UILabel *scroe;

@property (nonatomic, strong) NSMutableArray *homePlayerArray;

@property (nonatomic, strong) NSMutableArray *guestPlayerArray;

@end

@implementation ScroingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _width = 34*1.5;
    _height = 38*1.5;
    _originWidth = 34;
    _originHeight = 38;
    
    _switchHeight = 60*2;
    _switchOriginHeight = 60;
    _selectedTag = 1000;
//    [self initPlayerModel];
    [self initPlayerView];
}

#pragma  mark -创建球员数据模型
- (void)initPlayerModel
{
    _homePlayerArray = [NSMutableArray array];
    _guestPlayerArray = [NSMutableArray array];
    
    for (int i = 0; i < _homeTeamNum; i ++) {
        PlayerModel *homeModel = [[PlayerModel alloc] initWithName:@"" andPosition:@"" andPosNumber:1];
        homeModel.indexTag = i;
        [_homePlayerArray addObject:homeModel];
    }
    for (int j = 0 ; j < _guestTeamNum; j ++) {
        PlayerModel *guestModel = [[PlayerModel alloc] initWithName:@"" andPosition:@"" andPosNumber:1];
        guestModel.indexTag = j;
        [_guestPlayerArray addObject:guestModel];
    }
    
    
    
    for (int k = 0 ; k < 5; k ++) {
        PlayerModel *homeModel = [_homePlayerArray objectAtIndex:k];
        homeModel.starter = YES;
        PlayerModel *guestModel = [_guestPlayerArray objectAtIndex:k];
        guestModel.starter = YES;
    }
    
   
}

#pragma  mark -创建上场球员视图
- (void)initPlayerView
{
    [_homeC setdeTialwithbackground:[UIImage imageNamed:@"8.jpg"] PlayerNumber:@"12" PlayerModel:_homePlayerArray[4] andPositionNumber:5];
    
    [_homePF setdeTialwithbackground:[UIImage imageNamed:@"8.jpg"] PlayerNumber:@"12" PlayerModel:_homePlayerArray[3] andPositionNumber:4];
    
    [_homeSF setdeTialwithbackground:[UIImage imageNamed:@"8.jpg"] PlayerNumber:@"12" PlayerModel:_homePlayerArray[2] andPositionNumber:3];
    
    [_homeSG setdeTialwithbackground:[UIImage imageNamed:@"8.jpg"] PlayerNumber:@"12" PlayerModel:_homePlayerArray[1] andPositionNumber:2];
    
    [_homePG setdeTialwithbackground:[UIImage imageNamed:@"8.jpg"] PlayerNumber:@"12" PlayerModel:_homePlayerArray[0] andPositionNumber:1];
    
    _homeC.isHome = YES;
    _homePF.isHome = YES;
    _homeSF.isHome = YES;
    _homeSG.isHome = YES;
    _homePG.isHome = YES;
    
    [_guestC setdeTialwithbackground:[UIImage imageNamed:@"8.jpg"] PlayerNumber:@"12" PlayerModel:_guestPlayerArray[4] andPositionNumber:5];
    
    [_guestPF setdeTialwithbackground:[UIImage imageNamed:@"8.jpg"] PlayerNumber:@"12" PlayerModel:_guestPlayerArray[3] andPositionNumber:4];
    
    [_guestSF setdeTialwithbackground:[UIImage imageNamed:@"8.jpg"] PlayerNumber:@"12" PlayerModel:_guestPlayerArray[2] andPositionNumber:3];
    
    [_guestSG setdeTialwithbackground:[UIImage imageNamed:@"8.jpg"] PlayerNumber:@"12" PlayerModel:_guestPlayerArray[1] andPositionNumber:2];
    
    [_guestPG setdeTialwithbackground:[UIImage imageNamed:@"8.jpg"] PlayerNumber:@"12" PlayerModel:_guestPlayerArray[0] andPositionNumber:1];
    
}



- (void)addScaleAnimation
{
    for (UIView *view in _scoringTableArray) {
        CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnimation.fromValue = @1;
        scaleAnimation.toValue = @1.5;
        scaleAnimation.duration = 0.3;
        scaleAnimation.repeatCount = 1;
        scaleAnimation.autoreverses = NO;
        [view.layer addAnimation:scaleAnimation forKey:@"scaleAnimation"];
    }
}

#pragma mark -功能按钮
- (IBAction)beginAndFinish:(UIButton *)sender {
    [sender setTitle:@"结束" forState:UIControlStateNormal];
    
    if ([sender.titleLabel.text isEqualToString:@"结束"]) {
        NSLog(@"即将跳转");
    }
}


- (IBAction)goBack:(id)sender {
}


- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

#pragma mark -移动
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸开始");
    UITouch *touch = [touches anyObject];
    CGPoint beginPoint = [touch locationInView:self.view];
    _lastPoint = beginPoint;
    if (touch.view.tag >= 10 && touch.view.tag <=19) {
        _originRect = touch.view.frame;
    }
    NSLog(@"%@",touch.view);
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸移动");
    UITouch *touch = [touches anyObject];
    CGPoint movePoint = [touch locationInView:self.view];
    
    if (touch.view.tag >= 10 && touch.view.tag <=19) {
        
//        NSLog(@"%@",touch.view);
        
        [self movePlayer:movePoint andTouchView:touch.view];
        
        for (UILabel *label in _scoringTableArray) {
            if (CGRectIntersectsRect(label.frame, touch.view.frame)) {
                
                CGRect rect = CGRectIntersection(label.frame, touch.view.frame);
                
                if (rect.size.width*rect.size.height >= (_originWidth*_originHeight)/2) {
                    
                    if (label.tag == 20) {
                        if (label.frame.size.height != _switchHeight) {
                            
                            [UIView animateWithDuration:0.1 animations:^{
                                
                                CGRect rect = label.frame;
                                rect.size.height = _switchHeight;
                                label.bounds = rect;
                                
                            }];
                            _selectedTag = label.tag;
//                            NSLog(@"_selectedTag______%d",label.tag);
                        }
                    }else
                    {
                        if (label.frame.size.width != _width && label.frame.size.height != _height) {
                            [UIView animateWithDuration:0.1 animations:^{
                                
                                CGRect rect = label.frame;
                                rect.size.width = _width;
                                rect.size.height = _height;
                                label.bounds = rect;
                                
                            }];
                            _selectedTag = label.tag;
//                            NSLog(@"_selectedTag______%d",label.tag);
                        }
                    }
                }else
                {
                    //将选中置为一个不存在的值
                    _selectedTag = 1000;
                    
                    if (label.tag == 20) {
                        [UIView animateWithDuration:.1 animations:^{
                            CGRect rect = label.frame;
                            rect.size.height = _switchOriginHeight;
                            label.bounds = rect;
                        }];
                    }else
                    {
                        if (label.frame.size.width != _originWidth && label.frame.size.height != _originHeight) {
                            [UIView animateWithDuration:0.1 animations:^{
                                CGRect rect = label.frame;
                                rect.size.width = _originWidth;
                                rect.size.height = _originHeight;
                                
                                label.bounds = rect;
                            }];
                            
                        }
                    }
                }
//                NSLog(@"%@",NSStringFromCGRect(rect));
            }
            else{
                if (label.tag == 20) {
                    if (label.frame.size.height != _switchOriginHeight) {
                        [UIView animateWithDuration:0.1 animations:^{
                            CGRect rect = label.frame;
                            rect.size.height = _switchOriginHeight;
                            label.bounds = rect;
                        }];
                    }
                }else{
                    if (label.frame.size.width != _originWidth && label.frame.size.height != _originHeight) {
                        [UIView animateWithDuration:0.1 animations:^{
                            CGRect rect = label.frame;
                            rect.size.width = _originWidth;
                            rect.size.height = _originHeight;
                            label.bounds = rect;
                        }];
                    }
                }
            }
        }
        
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸结束");
    UITouch *touch = [touches anyObject];
    if (touch.view.tag >=10 && touch.view.tag <= 19) {
        
        _selectedPlayerTag = touch.view.tag;
        playerView *playerView = [self.view viewWithTag:_selectedPlayerTag];
        
        [UIView animateWithDuration:0.5 animations:^{
            touch.view.frame = _originRect;
            UILabel *label = [self.view viewWithTag:_selectedTag];
            CGRect rect = label.frame;
            if (_selectedTag == 20) {
                rect.size.height = _switchOriginHeight;
                label.bounds = rect;
            }
            else
            {
                rect.size.width = _originWidth;
                rect.size.height = _originHeight;
                label.bounds = rect;
            }
        }];
        if (_selectedTag>=20 && _selectedTag<=33) {
            
            
            switch (_selectedTag) {
                case 20://换人
                {
                    if (playerView.isHome) {
                        PlayerModel *model = _homePlayerArray[_switchTag];
                        playerView.playerData = model;
                        playerView.number.text = [NSString stringWithFormat:@"%ld",model.number];
                        playerView.foulNumber.text = [NSString stringWithFormat:@"%.0f",model.personalFoul];
                    }else
                    {
                        PlayerModel *model = _guestPlayerArray[_switchTag];
                        playerView.playerData = model;
                        playerView.number.text = [NSString stringWithFormat:@"%ld",model.number];
                        playerView.foulNumber.text = [NSString stringWithFormat:@"%.0f",model.personalFoul];
                    }
                }
                    break;
                case 21://失误
                {
                    playerView.playerData.turnover ++;
                    NSLog(@"turnover%.2f",playerView.playerData.turnover);
                }
                    break;
                case 22://篮板
                {
                    playerView.playerData.rebound ++;
                    NSLog(@"rebound%.2f",playerView.playerData.rebound);
                }
                    break;
                case 23://抢断
                {
                    playerView.playerData.steal ++;
                    NSLog(@"steal%.2f",playerView.playerData.steal);
                }
                    break;
                case 24://盖帽
                {
                    playerView.playerData.blockShot ++;
                    NSLog(@"blockShot%.2f",playerView.playerData.blockShot);
                }
                    break;
                case 25://助攻
                {
                    playerView.playerData.assist ++;
                    NSLog(@"assist%.2f",playerView.playerData.assist);
                }
                    break;
                case 26://个人犯规
                {
                    playerView.playerData.personalFoul ++;
                    NSLog(@"personFoul%.2f",playerView.playerData.personalFoul);
                }
                    break;
                case 27://罚球
                {
                    playerView.playerData.andOne ++;
                    NSLog(@"andOne%.2f",playerView.playerData.andOne);
                    playerView.playerData.fieldGoalAttemptedAndOne ++;
                    playerView.playerData.shotOne ++;
                }
                    break;
                case 28://罚球不中
                {
                    playerView.playerData.fieldGoalAttemptedAndOne ++;
                }
                    break;
                case 29://两分
                {
                    playerView.playerData.twoPoint +=2;
                    NSLog(@"twoPoint%.2f",playerView.playerData.twoPoint);
                    playerView.playerData.fieldGoalAttemptedTwo ++;
                    playerView.playerData.shotTwo ++;
                }
                    break;
                case 30://两分不中
                {
                    playerView.playerData.fieldGoalAttemptedTwo++;
                }
                    break;
                case 31://三分
                {
                    playerView.playerData.threePoint += 3;
                    NSLog(@"threePoint%.2f",playerView.playerData.threePoint);
                    playerView.playerData.fieldGoalAttemptedThree ++;
                    playerView.playerData.shotThree ++;
                }
                    break;
                case 32://三分不中
                {
                    playerView.playerData.fieldGoalAttemptedThree ++;
                }
                    break;
                case 33://技术犯规
                {
                    playerView.playerData.technicalFoul ++;
                }
                    break;
                case 34://恶意犯规
                {
                    playerView.playerData.flagrantFoul ++;
                }
                    break;
                default:
                    break;
            }
        }
        
        
    }
}

- (void)movePlayer:(CGPoint)currentPoint andTouchView:(UIView *)view
{
    float playerPointX = currentPoint.x - _lastPoint.x;
    float playerPointY = currentPoint.y - _lastPoint.y;
    
    float newPointX = view.frame.origin.x + playerPointX;
    float newPointY = view.frame.origin.y + playerPointY;
   
    if (newPointX < 0) {
        newPointX = 0;
    }
    if (newPointX >kSCREENWIDTH - view.bounds.size.width) {
        newPointX = kSCREENWIDTH - view.bounds.size.width;
    }
    if (newPointY < 0) {
        newPointY = 0;
    }
    if (newPointY > kSCREENHEIGHT) {
        newPointY = kSCREENHEIGHT;
    }
    view.frame = CGRectMake(newPointX, newPointY, view.bounds.size.width, view.bounds.size.height);
    _lastPoint = currentPoint;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
